package com.humngyang.com.hmy.study.springtest.mybatis;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;

public class HmyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        // 不能将userDao传进去，如果将userDao传进去的话，那么就是交给spring来new一个对象，
        // 而mybatis没有让spring来new对象，只是让其来管理对象
//        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(HmyFracoryBean.class);
//        GenericBeanDefinition beanDefinition = (GenericBeanDefinition) bdb.getBeanDefinition();
//        String str = "com.humngyang.com.hmy.service.springtest.mybatis.RoleDao";
//        beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(str);
//        beanDefinitionRegistry.registerBeanDefinition("roleDao", beanDefinition);


        List<String> strList = new ArrayList<String>();
        strList.add("com.humngyang.com.hmy.study.springtest.mybatis.RoleDao");
        strList.add("com.humngyang.com.hmy.study.springtest.mybatis.UserDao");
        for (String s : strList) {
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(HmyFracoryBean.class);
            GenericBeanDefinition beanDefinition = (GenericBeanDefinition) bdb.getBeanDefinition();
            String sub = s.substring(s.length() - 7, s.length());
            beanDefinition.getConstructorArgumentValues().addGenericArgumentValue(s);
            beanDefinitionRegistry.registerBeanDefinition(sub, beanDefinition);
        }
    }
}
